package com.moore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moore.bean.Industry;
import com.moore.bean.Position;
import com.moore.mapper.IndustryMapper;
import com.moore.mapper.PositionMapper;
import com.moore.service.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class IndustryServiceImpl implements IndustryService {
    @Autowired
    private IndustryMapper industryMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Override
    public List<Industry> findAll() {
        // 查询所有industry表的内容
        List<Industry> industries = industryMapper.selectList(null);

        // 获取所有position表中industry_id对应的内容
        List<Long> industryIds = industries.stream().map(Industry::getIndustryId).collect(Collectors.toList());
        QueryWrapper<Position> queryWrapper = Wrappers.query();
        queryWrapper.in("industry_id", industryIds);
        List<Position> positions = positionMapper.selectList(queryWrapper);

        // 将positions按industry_id分组并设置到对应的industry对象中
        industries.forEach(industry -> {
            List<Position> relatedPositions = positions.stream()
                    .filter(position -> position.getIndustryId() == industry.getIndustryId()) // 使用==进行比较
                    .collect(Collectors.toList());
            industry.setPositions(relatedPositions);
        });


        return industries;
    }
}
